package com.gykj.tap.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.gykj.tap.service.IFanService;
import com.serotonin.modbus4j.exception.ErrorResponseException;
import com.serotonin.modbus4j.exception.ModbusInitException;
import com.serotonin.modbus4j.exception.ModbusTransportException;

@Api(value="风机操作接口")
@RestController
@Slf4j
public class FanController {
	
	@Resource
	IFanService fanservice;
	
	
	@ApiOperation(value = "查看设备状态" ,  notes="查看风机的状态")
    @RequestMapping(value="/ViewStatus",method=RequestMethod.GET)
    public Map<String,Object> ViewStatus(/*@RequestParam(value = "tunnelId",required = false) Integer tunnelId,
    		@RequestParam(value = "deviceId",required = false) Integer deviceId,
    		@RequestParam(value = "typeCode",required = false) Integer typeCode*/) {
    	
    	Map<String,Object> map = new HashMap<>();
//    	String host ="192.168.28.99";
    	String host ="localhost";
        int port = 502;
    	try {
			Boolean result = fanservice.readCoilStatus(host, port, 1, 0);
			map.put("status", result);
			map.put("message", "");
			log.info("风机返回的结果： "+result);
			
		} catch (ModbusTransportException e) {
			e.printStackTrace();
			map.put("status", false);
			map.put("message", e.getMessage());
		} catch (ErrorResponseException e) {
			e.printStackTrace();
			map.put("status", false);
			map.put("message", e.getMessage());
		} catch (ModbusInitException e) {
			e.printStackTrace();
			map.put("status", false);
			map.put("message", e.getMessage());
		}
    	return map;
    }
}
